#start to collate data
captureSNPs <- function(contig.map,contig.dataframes,cDNA.name,AceCOs,ace.files,envir=.GlobalEnv){
	#a wrapper to take SNP data from multiple populations
	#contig map is a list derived from join contigs
	#contig.dataframes is a character vector containing names of dataframes
	# produced by readNewblerMapper
	#cDNA.name is the name of the cDNA-derived supercontig
	#AceCOs is a character vector containing names of dataframes for CO line
	#numbers in ace files produced by readAceCO
	#ace.files is a character vector containing names of ace files to be read
	#by readContigAce
	#envir is the environment in which to look for contig.dataframes & AceCOs
	
	SNPlist <- list()
	AllCoverage <- list()
	
	for(i in 1:length(contig.dataframes)){
		contig.df <- get(contig.dataframes[i],envir=envir,inherits=FALSE)
		SNPlist[[i]] <- mapSNPsToContigs(contig.df,contig.map,cDNA.name)
		if(!identical(SNPlist[[i]],0)){
			SNPlist[[i]]$SNPid <- paste(cDNA.name,SNPlist[[i]]$start.pos,SNPlist[[i]]$ref.accno,SNPlist[[i]]$old.start,SNPlist[[i]]$ref.nucl,SNPlist[[i]]$var.nucl,sep="_")
			}else{
			SNPlist[[i]]$SNPid <- NULL
			}
		
		AceCO <- get(AceCOs[i],envir=envir,inherits=FALSE)
		ace.file<-ace.files[i]
		linesAceList <- lapply(X=contig.map[[cDNA.name]]$sc.contig,
			FUN=function(X,ace.file,AceCO){
				readContigAce(ace.file,AceCO,X)
				},ace.file,AceCO)
		AceObjectList <- lapply(X= linesAceList,FUN=makeAceObject)
		CoverageList <- lapply(X= AceObjectList,FUN= mapCoverage)
		rm(linesAceList, AceObjectList)
		names(CoverageList) <- contig.map[[cDNA.name]]$sc.contig
		#should now have an estimate of the coverage at each base
		AllCoverage[[i]] <- CoverageList
		
		}
	names(SNPlist) <- contig.dataframes
	names(AllCoverage) <- contig.dataframes
	AllSNPids <- sort(unique(unlist(sapply(X=SNPlist,FUN=function(X){
		X$SNPid
		}))))
	list(SNPlist = SNPlist ,AllCoverage=AllCoverage,AllSNPids=AllSNPids)
	}


debug(captureSNPs)
captureSNPs(contig.map,c("grouse1hc","grouse2hc"),names(contig.map[2]),c("grouse1CO","grouse2CO"),c("/Paterson/Datafiles/grouse/mapping/grouse1.ace","/Paterson/Datafiles/grouse/mapping/grouse2.ace"))

baseCoverage <- function(inputlist){
	SNPlist <- inputlist[['SNPlist']]
	AllCoverage <- inputlist[['AllCoverage']]
	AllSNPids <- inputlist[['AllSNPids']]
	if(is.null(AllSNPids)) return(0)
	
	if(all(sapply(X=AllCoverage,function(X){length(X)==0}))){
		AllSNPref <- 0
		return(AllSNPref)
		}
	if(any(sapply(AllCoverage,length)/length(AllCoverage[[1]])!=1)) stop('different numbers of contigs in AllCoverage')
	
	if(substr(AllSNPids[1],1,6)=="Contig"){
		AllSNPref <- data.frame(id = AllSNPids ,cDNA = sapply(X=strsplit(AllSNPids,"_"),FUN=function(X){
			X[1]
			}),
			start.pos = as.numeric(sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[2]
			})),
			contig = sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[3]
			}),
			old.pos = as.numeric(sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[4]
			})),stringsAsFactors=FALSE)

		}else{
		AllSNPref <- data.frame(id = AllSNPids ,cDNA = sapply(X=strsplit(AllSNPids,"_"),FUN=function(X){
		paste(X[1],X[2],sep="_")
			}),
			start.pos = as.numeric(sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[3]
			})),
			contig = sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[4]
			}),
			old.pos = as.numeric(sapply(strsplit(AllSNPids,"_"),FUN=function(X){
				X[5]
			})),stringsAsFactors=FALSE)

		}
	
	AllSNPref[,paste(names(AllCoverage),"cov",sep=".")] <- 0
	AllSNPref[,paste(names(SNPlist),"freq",sep=".")] <- -1
	AllSNPref[,paste(names(SNPlist),"depth",sep=".")] <- -1
	#str of AllCoverage is popn > contig > unpadded/padded then dataframes
	for(pop in names(AllCoverage)){
		for(pos in 1:dim(AllSNPref)[1]){
			AllSNPref[pos,paste(pop,"cov",sep=".")] <- pickCoverage(AllCoverage[[pop]],
				contig=AllSNPref[pos,'contig'],old.pos=AllSNPref[pos,'old.pos'])
			AllSNPref[pos,paste(pop,"freq",sep=".")] <- pickSNP(SNPlist[[pop]],id= AllSNPref[pos,'id'])
			AllSNPref[pos,paste(pop,"depth",sep=".")] <- pickDepth(SNPlist[[pop]],id= AllSNPref[pos,'id'])
			}#must be a vector method to do this...
		}
	
	AllSNPref[order(AllSNPref$start.pos),]
	}

pickCoverage <- function(CoverageList, contig,old.pos){
	tmp <- CoverageList[[contig]][['coverage']][old.pos,'coverage']
	if(is.null(tmp)){
		return(0)
		}
	tmp
	}

pickSNP <- function(snp.df, id){
	if(!is.na(match(id,snp.df$SNPid))>0){
		snp.freq <- snp.df[match(id,snp.df$SNPid),'freq']
		snp.freq <- as.numeric(sub("%$","",snp.freq))
		}else{
		snp.freq <- 0
		}
	snp.freq
	}
pickDepth <- function(snp.df, id){
	if(!is.na(match(id,snp.df$SNPid))>0){
		snp.depth <- snp.df[match(id,snp.df$SNPid),'depth']
		snp.depth <- as.numeric(snp.depth)
		}else{
		snp.depth <- 0
		}
	snp.depth
	}
